package edu.pitt.dbmi.odie.gapp.gwt.client.document;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.types.DSDataFormat;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.events.RecordDoubleClickEvent;
import com.smartgwt.client.widgets.grid.events.RecordDoubleClickHandler;
import com.smartgwt.client.widgets.layout.VLayout;

public class ODIE_DocumentsPanel extends VLayout {

	/**
	 * Create a remote service proxy to talk to the server-side ODIE_ObaService
	 * service.
	 */
	private final ODIE_ObaServiceAsync obaService = GWT
			.create(ODIE_ObaService.class);
	
    public ODIE_DocumentsPanel() {
		setLayoutMargin(10) ;
		DataSource analysesDS = buildDataSource() ;
		VLayout analysesGroup = buildGroup("Analyses", analysesDS) ;
		addMember(analysesGroup);
	}
	
	private VLayout buildGroup(String groupName, DataSource dataSource) {
	
		ListGrid listGrid = null ;
		listGrid = buildListGrid() ;
		listGrid.setDataSource(dataSource) ;
		listGrid.addRecordDoubleClickHandler(buildDirectoryNameRecordDoubleClickListener());
//		listGrid.setSortField("title") ;
		
		VLayout group = new VLayout();
		group.setLayoutMargin(10) ;
		group.setIsGroup(true);
		group.setGroupTitle(groupName);
		group.addMember(listGrid) ;
		return group ;
	}
	
	private ListGrid buildListGrid() {
		ListGrid grid = new ListGrid();
		grid.setWidth100();
		grid.setHeight100();
		grid.setAutoFetchData(true);
		return grid ;
	}
	
	private DataSource buildDataSource() {
		DataSource dataSource = new DataSource();
		dataSource.setDataFormat(DSDataFormat.XML);
		dataSource.setRecordXPath("//atom:entry");
		dataSource.setDataURL("odie_client/document");
		DataSourceTextField idField = new DataSourceTextField(
				"id", "id");
		idField.setHidden(true) ;
		DataSourceTextField displayLabelField = new DataSourceTextField(
				"title", "Title");
		dataSource.setFields(idField, displayLabelField);
		return dataSource ;
	}
	
	private RecordDoubleClickHandler buildDirectoryNameRecordDoubleClickListener() {
		return new RecordDoubleClickHandler() {
            public void onRecordDoubleClick(RecordDoubleClickEvent event) {
            	Record record = event.getRecord() ;
            	String directoryHandle = record.getAttribute("id") ;
            	String title = record.getAttribute("title") ;
            	autoCodeDirectory(directoryHandle, title) ;
            }
        } ;
	}
	
	private void autoCodeDirectory(String directoryHandle, String title) {
		obaService.autoCode(directoryHandle, title,
				new AsyncCallback<String>() {
					public void onFailure(Throwable error) {
						SC.say("Coding failed.") ;
					}
					public void onSuccess(String result) {
						SC.say("Coding is complete!") ;
					}
				});
		
	}
	

	
	

}
